// EventLogPane.cpp : implementation file
//

#include "stdafx.h"
#include "WebGUI.h"
#include "EventLogPane.h"

IMPLEMENT_DYNAMIC(CEventLogPane, CDockablePane)

CEventLogPane::CEventLogPane()
{

}

CEventLogPane::~CEventLogPane()
{
	m_Font.DeleteObject() ;

}


BEGIN_MESSAGE_MAP(CEventLogPane, CDockablePane)
	ON_WM_PAINT()
	ON_WM_SETFOCUS()
	ON_WM_SIZE()
	ON_WM_CREATE()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


void CEventLogPane::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	CRect rectTree;
	m_ctrlEventList.GetWindowRect(rectTree);
	ScreenToClient(rectTree);

	rectTree.InflateRect(1, 1);
	dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
}

void CEventLogPane::OnSetFocus(CWnd* pOldWnd)
{
	CDockablePane::OnSetFocus(pOldWnd);
	m_ctrlEventList.SetFocus();
}

void CEventLogPane::OnSize(UINT nType, int cx, int cy)
{
	CDockablePane::OnSize(nType, cx, cy);

	if (CanAdjustLayout())
	{
		CRect rc;
		GetClientRect(rc);

		m_ctrlEventList.SetWindowPos(NULL, rc.left+0 , rc.top+0 , rc.Width()-0 , rc.Height()-0 , SWP_NOACTIVATE | SWP_NOZORDER );

		CRect rcList ;
		m_ctrlEventList.GetClientRect ( rcList ) ;
		for ( int i = 0 ; i < 3 ; i++ ) 
			rcList.DeflateRect ( m_ctrlEventList.GetColumnWidth(i) , 0 , 0 , 0 ) ;
		if ( rcList.Width() <= 300 )
			m_ctrlEventList.SetColumnWidth ( 3 , 300 ) ;
		else
			m_ctrlEventList.SetColumnWidth ( 3 , rcList.Width() - 16 ) ;
	}
}

int CEventLogPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_Font.CreateStockObject(DEFAULT_GUI_FONT);

	CRect rectDummy;
	rectDummy.SetRectEmpty();

	const DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_VSCROLL | LVS_REPORT | WS_BORDER ;

	m_ctrlEventList.Create(dwStyle, rectDummy, this, 1);
	m_ctrlEventList.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	m_ctrlEventList.InsertColumn(1, _T("Type"), LVCFMT_LEFT, 60);
	m_ctrlEventList.InsertColumn(2, _T("Event Time"), LVCFMT_LEFT, 140);
	m_ctrlEventList.InsertColumn(3, _T("Description"), LVCFMT_LEFT, 300);

	m_ctrlEventList.SetFont(&m_Font);

	{
		m_ImageList.Create ( 16 , 16 , ILC_MASK|ILC_COLOR32 , 12 , 0 ) ;


		m_ImageList.Add ( AfxGetApp()->LoadIcon(IDI_ICON1) );
		m_ImageList.Add ( AfxGetApp()->LoadIcon(IDI_ICON2) );
		m_ImageList.Add ( AfxGetApp()->LoadIcon(IDI_ICON3) );
		m_ImageList.Add ( AfxGetApp()->LoadIcon(IDI_ICON4) );

		m_ctrlEventList.SetImageList ( &m_ImageList , LVSIL_SMALL ) ;
	}

	return 0;
}





void CEventLogPane::OnDestroy()
{
	CDockablePane::OnDestroy();
	m_ImageList.DeleteImageList() ;

}

void CEventLogPane::AddLog( CString strEventType , CString strDesciption )
{
	int nIndex = m_ctrlEventList.GetItemCount() ;
	int nImage = 2;
	if (strEventType=="Report")
	{
		nImage = 2;
	}
	if (strEventType=="Warning")
	{
		nImage = 0;
	}
	if (strEventType=="Error")
	{
		nImage = 1;
	}


	m_ctrlEventList.InsertItem( nIndex, strEventType ,nImage) ;
	CString strTime ;
	SYSTEMTIME st ;
	GetLocalTime(&st);
	strTime.Format("%4d/%02d/%02d - %02d:%02d:%02d", st.wYear,
		st.wMonth,
		st.wDay,
		st.wHour,
		st.wMinute,
		st.wSecond) ;
	m_ctrlEventList.SetItemText(nIndex,1, strTime ) ;
	m_ctrlEventList.SetItemText(nIndex,2, strDesciption ) ;
}